package zy.blue7.gui.netty.custom;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @Author: blue7
 * @Date: 2022/2/27 14:12
 * @Version 1.0
 * @Description
 */
public class Client {

    public static void main(String[] args) throws InterruptedException {
        new Bootstrap()
                // 2.添加 EventLoop
                .group(new NioEventLoopGroup())
                //3. 选择客户端 channel实现
                .channel(NioSocketChannel.class)
                //4.添加处理器
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override // todo 在建立链接的时候调用
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new Blue7MessageEncoder());
                        ch.pipeline().addLast(new Blue7MessageDecoder());
                        ch.pipeline().addLast(new SimpleChannelInboundHandler<Blue7Message>() {
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                for (int i = 0; i < 6; i++) {
                                    String content="hello  ni hao ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"+i;
                                    Blue7Header blue7Header = Blue7Header.builder()
                                            .version(1)
                                            .contentLength(content.length())
                                            .sessionId("000" + i).build();

                                    Blue7Message blue7Message = Blue7Message.builder()
                                            .blue7Header(blue7Header)
                                            .content(content).build();

                                    ctx.writeAndFlush(blue7Message);
                                }
                            }

                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, Blue7Message blue7Message) throws Exception {
                                System.out.println(blue7Message.getBlue7Header());
                                System.out.println(blue7Message);
                            }
                        });

                    }
                })
                //5.连接到服务器
                .connect(new InetSocketAddress("localhost", 8080))
                .sync() //todo 阻塞方法，知道链接建立
                .channel();
//                .writeAndFlush("nihao");


    }
}
